package com.myopen.controller;

import java.util.Date;
import java.util.Random;

import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.kit.DateKit;
import com.myopen.kit.DeviceKit;
import com.myopen.kit.UUIDKit;
import com.myopen.model.Order;

public class IndexController extends BaseController{

	public void index(){
		redirect("/admin");
	}
	
	@Clear
	@ActionKey("/captcha")
	public void captcha() {
		renderCaptcha();
	}

	@Clear
	@ActionKey("/qrCode")
	public void qrCode(){
		
		int width=getParaToInt("width");
		int height=getParaToInt("height");
		String qrCodeContent = getPara("qrCodeContent");
        renderQrCode(qrCodeContent, width, height);

	}

	@ActionKey("/test/shopping")
	public void test_shopping(){
		String orderNumber=DateKit.format(new Date(), "yyyyMMddHHmmss"+new Random().nextInt(2));
		Record order=new Record();
		order.set("id", UUIDKit.getUUID());
		order.set("removed","0");
		order.set("createTime",new Date());
		order.set("updateTime", new Date());
		order.set("number", orderNumber);
		//TODO价格这里不安全
		order.set("amount", 0.01D);
		order.set("status", "1");
		order.set("userId", "1234567");
		Db.save("t_shop_order", order);

		setAttr("order", order);
		setAttr("isMobileDevice", DeviceKit.isMobileDevice(getRequest()));
		setAttr("isWeChat", DeviceKit.isWeChat(getRequest()));
		render("pay.html");
	}

	@ActionKey("/paySuccess")
	public void paySuccess(){
		
		System.out.println(getPara("out_trade_no"));

		String out_trade_no=getPara("out_trade_no");
		Record payHis=Order.dao.getPayHistory(out_trade_no);
		Record order=Order.dao.getRecordById(payHis.getStr("orderId"));
		
		setAttr("order", order);
		render("/pay.html");

	}


}
